Merge pull request #925 from cantino/scenarios_contain_control_links

Scenarios can contain control links

Andrew Cantino 9 gadi atpakaļ
vecāks
revīzija
2f6fa93475

+ 2 - 0
CHANGES.md

@@ -1,5 +1,7 @@
1 1
 # Changes
2 2
 
3
+* Jul 20, 2015   - Control Links (used by the SchedularAgent) are correctly exported in Scenarios.
4
+* Jul 20, 2015   - keep\_events\_for was moved from days to seconds; Scenarios have a schema verison.
3 5
 * Jul 1, 2015    - DeDuplicationAgent properly handles destruction of memory.
4 6
 * Jun 26, 2015   - Add `max_events_per_run` to RssAgent.
5 7
 * Jun 19, 2015   - Add `url_from_event` to WebsiteAgent.

+ 8 - 0
app/models/scenario_import.rb

@@ -60,6 +60,7 @@ class ScenarioImport
60 60
     description = parsed_data['description']
61 61
     name = parsed_data['name']
62 62
     links = parsed_data['links']
63
+    control_links = parsed_data['control_links'] || []
63 64
     tag_fg_color = parsed_data['tag_fg_color']
64 65
     tag_bg_color = parsed_data['tag_bg_color']
65 66
     source_url = parsed_data['source_url'].presence || nil
@@ -87,12 +88,19 @@ class ScenarioImport
87 88
         end
88 89
         agent
89 90
       end
91
+
90 92
       if success
91 93
         links.each do |link|
92 94
           receiver = created_agents[link['receiver']]
93 95
           source = created_agents[link['source']]
94 96
           receiver.sources << source unless receiver.sources.include?(source)
95 97
         end
98
+
99
+        control_links.each do |control_link|
100
+          controller = created_agents[control_link['controller']]
101
+          control_target = created_agents[control_link['control_target']]
102
+          controller.control_targets << control_target unless controller.control_targets.include?(control_target)
103
+        end
96 104
       end
97 105
     end
98 106
 

+ 16 - 3
lib/agents_exporter.rb

@@ -21,7 +21,8 @@ class AgentsExporter
21 21
       :tag_bg_color => options[:tag_bg_color],
22 22
       :exported_at => Time.now.utc.iso8601,
23 23
       :agents => agents.map { |agent| agent_as_json(agent) },
24
-      :links => links
24
+      :links => links,
25
+      :control_links => control_links
25 26
     }
26 27
   end
27 28
 
@@ -33,14 +34,26 @@ class AgentsExporter
33 34
     agent_ids = agents.map(&:id)
34 35
 
35 36
     contained_links = agents.map.with_index do |agent, index|
36
-      agent.links_as_source.where(:receiver_id => agent_ids).map do |link|
37
-        { :source => index, :receiver => agent_ids.index(link.receiver_id) }
37
+      agent.links_as_source.where(receiver_id: agent_ids).map do |link|
38
+        { source: index, receiver: agent_ids.index(link.receiver_id) }
38 39
       end
39 40
     end
40 41
 
41 42
     contained_links.flatten.compact
42 43
   end
43 44
 
45
+  def control_links
46
+    agent_ids = agents.map(&:id)
47
+
48
+    contained_controller_links = agents.map.with_index do |agent, index|
49
+      agent.control_links_as_controller.where(control_target_id: agent_ids).map do |control_link|
50
+        { controller: index, control_target: agent_ids.index(control_link.control_target_id) }
51
+      end
52
+    end
53
+
54
+    contained_controller_links.flatten.compact
55
+  end
56
+
44 57
   def agent_as_json(agent)
45 58
     {
46 59
       :type => agent.type,

+ 8 - 0
spec/lib/agents_exporter_spec.rb

@@ -26,6 +26,7 @@ describe AgentsExporter do
26 26
       expect(data[:tag_bg_color]).to eq(tag_bg_color)
27 27
       expect(Time.parse(data[:exported_at])).to be_within(2).of(Time.now.utc)
28 28
       expect(data[:links]).to eq([{ :source => 0, :receiver => 1 }])
29
+      expect(data[:control_links]).to eq([])
29 30
       expect(data[:agents]).to eq(agent_list.map { |agent| exporter.agent_as_json(agent) })
30 31
       expect(data[:agents].all? { |agent_json| agent_json[:guid].present? && agent_json[:type].present? && agent_json[:name].present? }).to be_truthy
31 32
 
@@ -39,6 +40,13 @@ describe AgentsExporter do
39 40
 
40 41
       expect(exporter.as_json[:links]).to eq([{ :source => 0, :receiver => 1 }])
41 42
     end
43
+
44
+    it "outputs control links to agents within the incoming set, but not outside it" do
45
+      agents(:jane_rain_notifier_agent).control_targets = [agents(:jane_weather_agent), agents(:jane_basecamp_agent)]
46
+      agents(:jane_rain_notifier_agent).save!
47
+
48
+      expect(exporter.as_json[:control_links]).to eq([{ :controller => 1, :control_target => 0 }])
49
+    end
42 50
   end
43 51
 
44 52
   describe "#filename" do

+ 34 - 1
spec/models/scenario_import_spec.rb

@@ -75,7 +75,8 @@ describe ScenarioImport do
75 75
       ],
76 76
       :links => [
77 77
         { :source => 0, :receiver => 1 }
78
-      ]
78
+      ],
79
+      :control_links => []
79 80
     }
80 81
   end
81 82
   let(:valid_data) { valid_parsed_data.to_json }
@@ -244,6 +245,38 @@ describe ScenarioImport do
244 245
             expect(trigger_agent.keep_events_for).to eq(0)
245 246
           end
246 247
         end
248
+
249
+        describe "with control links" do
250
+          it 'creates the links' do
251
+            valid_parsed_data[:control_links] = [
252
+              { :controller => 1, :control_target => 0 }
253
+            ]
254
+
255
+            expect {
256
+              scenario_import.import
257
+            }.to change { users(:bob).agents.count }.by(2)
258
+
259
+            weather_agent = scenario_import.scenario.agents.find_by(:guid => "a-weather-agent")
260
+            trigger_agent = scenario_import.scenario.agents.find_by(:guid => "a-trigger-agent")
261
+
262
+            expect(trigger_agent.sources).to eq([weather_agent])
263
+            expect(weather_agent.controllers.to_a).to eq([trigger_agent])
264
+            expect(trigger_agent.control_targets.to_a).to eq([weather_agent])
265
+          end
266
+
267
+          it "doesn't crash without any control links" do
268
+            valid_parsed_data.delete(:control_links)
269
+
270
+            expect {
271
+              scenario_import.import
272
+            }.to change { users(:bob).agents.count }.by(2)
273
+
274
+            weather_agent = scenario_import.scenario.agents.find_by(:guid => "a-weather-agent")
275
+            trigger_agent = scenario_import.scenario.agents.find_by(:guid => "a-trigger-agent")
276
+
277
+            expect(trigger_agent.sources).to eq([weather_agent])
278
+          end
279
+        end
247 280
       end
248 281
 
249 282
       describe "#generate_diff" do